home *** CD-ROM | disk | FTP | other *** search
/ PC World Interactive 7 / PC World Interactive 7.iso / program / cprog.EXE / BIP.C < prev    next >
C/C++ Source or Header  |  1991-03-08  |  8KB  |  220 lines

  1. /* I wrote BIP because of a need to find specific sounds for my programming
  2.    needs. With this somewhat crude program I can find any sound I want without
  3.    programming and then later include the proper settings in my program. The
  4.    title BIP came from 'The Pink Panther'. The inspector would pronounce the
  5.    word BEEP as BIP.
  6.  
  7.    This program is given free of charge to everyone, without any warranty
  8.    expressed or implied. I will in no way be held responsible for any damages
  9.    of any kind (real or imaginary) caused by the use or misuse of this
  10.    program.
  11.  
  12.    Feel free to use any part of the following code for any purpose you
  13.    see fit. Just remember to leave the copyright notice in the BIP program.
  14.    Give credit where credit is due.
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <dos.h>
  19.  
  20. #define  COPYRIGHT1 "B I P >> Beep Tester, Version 2.0\n"
  21. #define  COPYRIGHT2 "(C) Copyright 1989, John Carto, All Rights Reserved."
  22. #define  BACKGROUND set_attr(0x20,0x07,0x01,0x50)/* WHITE ON BLUE BACKGROUND */
  23. #define  WAIT       set_curs(23,0); /* A PLACE TO PRINT MESSAGES */
  24. #define  UP_ARO     0x48
  25. #define  DN_ARO     0x50
  26. #define  SELECT     0x3B           /* F1 KEY */
  27. #define  NUMPUT     0x3C           /* F2 KEY */
  28. #define  STAT1      set_curs(0,25);
  29. #define  STAT2      set_curs(0,51);
  30. #define  STATS      "\x1B[30;46m              Frequency = 500.00  Hz    Duration = 0.5 Seconds               \x1B[37;44m"
  31. #define  HELP       "\x1B[30;46m     F1=Freq./Duration, F2=Manual Input, ENTER=Tone, UP/DN=Fine Tune, ESC=Exit     \x1B[37;44m\x1B[24;1f"
  32. #define  REV1       "Freq.\x1B[37;44m"
  33. #define  REV2       "Duration,\x1B[37;44m"
  34. #define  REV3       "Manual Input,\x1B[37;44m"
  35. #define  REV4       "Tone,\x1B[37;44m"
  36. #define  REV5       "Fine Tune,\x1B[37;44m"
  37. #define  BROWN      "\x1B[30;43m"
  38. #define  CYAN       "\x1B[30;46m"
  39. #define  TMODE      0xB6
  40. #define  FREQSCALE  1193820L
  41. #define  TIMESCALE  40000L
  42. #define  TMODE_PORT 0x43
  43. #define  FREQ_PORT  0x42
  44. #define  BEEP_PORT  0x61
  45. #define  ON         0x4F
  46.  
  47.         float  freq=500.00, duration=0.5;/* PRESET THE FREQUENCY & DURATION */
  48.  
  49.  
  50. main ()
  51.  
  52. {
  53.  
  54.         int   chk, code, OK;
  55.         int   i, j;
  56.  
  57.         set_curs(0,0);
  58.         for(i=0;i<25;i++) { set_curs(i,0); (BACKGROUND);} /* COLOR ON */
  59.         set_curs(0,0);
  60.         printf(STATS);
  61.         printf(HELP);
  62.         printf(COPYRIGHT1);
  63.         printf(COPYRIGHT2);
  64.         set_curs(1,5); printf(BROWN); printf(REV1);
  65.         WAIT
  66.         chk = 1;
  67.         while (1)
  68.           { code = getcode();
  69.             switch (code)
  70.               { case UP_ARO: /* INCREMENT THE CURRENT VARIABLE */
  71.                         set_curs(1,24); printf(CYAN); printf(REV3);
  72.                         set_curs(1,56); printf(BROWN); printf(REV5);
  73.                         if(chk == 1 && freq < 30000.00)
  74.                           { freq += 0.01; printf(CYAN);
  75.                             STAT1 printf("%7.2f\x1B[37;44m", freq);
  76.                           }
  77.                         else if(chk != 1 && duration < 5.0)
  78.                           { duration += 0.1; printf(CYAN);
  79.                             STAT2 printf("%2.1f\x1B[37;44m", duration);
  80.                           }
  81.                         WAIT
  82.                         break;
  83.                 case DN_ARO: /* DECREMENT THE CURRENT VARIABLE */
  84.                         set_curs(1,24); printf(CYAN); printf(REV3);
  85.                         set_curs(1,56); printf(BROWN); printf(REV5);
  86.                         if(chk == 1 && freq > 0.01)
  87.                           { freq -= 0.01; printf(CYAN); STAT1
  88.                             printf("%7.2f\x1B[37;44m", freq);
  89.                           }
  90.                         else if(chk != 1 && duration > 0.2)
  91.                           { duration -= 0.1; printf(CYAN);
  92.                             STAT2 printf("%2.1f\x1B[37;44m", duration);
  93.                           }
  94.                         WAIT
  95.                         break;
  96.                 case SELECT: /* SELECT THE CURRENT VARIABLE */
  97.                         if(chk == 1)
  98.                          { chk = -1;
  99.                            set_curs(1,5); printf(CYAN); printf(REV1);
  100.                            set_curs(1,11); printf(BROWN); printf(REV2);
  101.                            WAIT
  102.                          }
  103.                         else
  104.                          { chk = 1;
  105.                            set_curs(1,5); printf(BROWN); printf(REV1);
  106.                            set_curs(1,11); printf(CYAN); printf(REV2);
  107.                            WAIT
  108.                          }
  109.                         break;
  110.                 case NUMPUT:               /* MANUAL INPUT */
  111.                         set_curs(1,24); printf(BROWN); printf(REV3);
  112.                         set_curs(1,56); printf(CYAN); printf(REV5);
  113. retry:                  WAIT
  114.                         set_attr(0x20,0x07,0x01,0x50);  /* ERASE LINE */
  115.                         if(chk == 1)
  116.                          { printf("Enter Desired Frequency: ");
  117.                            scanf("%f", &freq);
  118.                                    fflush(stdin);
  119.                            if(freq < 30000.01 && freq > 0.01)
  120.                             { printf(CYAN); STAT1
  121.                               printf("%7.2f\x1B[37;44m", freq);
  122.                             }
  123.                            else
  124.                             goto retry;
  125.                          }
  126.                         else 
  127.                          { printf("Enter Desired Duration: ");
  128.                            scanf("%f", &duration);
  129.                            fflush(stdin);
  130.                            if(duration < 5.1 && duration > 0.1)
  131.                             { printf(CYAN); STAT2
  132.                               printf("%2.1f\x1B[37;44m", duration);
  133.                             }
  134.                            else
  135.                             goto retry;
  136.                          }
  137.                         set_curs(1,24); printf(CYAN); printf(REV3);
  138.                         WAIT;
  139.                         break;
  140.                 case 13:        /* ENTER KEY, BEEP ACCORDING TO SETTINGS */
  141.                         set_curs(1,56); printf(CYAN); printf(REV5);
  142.                         set_curs(1,44); printf(BROWN); printf(REV4);
  143.                         tone();
  144.                         set_curs(1,44); printf(CYAN); printf(REV4);
  145.                         WAIT
  146.                         break;
  147.                 case 27:        /* ESC KEY, EXIT BIP */
  148.                         goto leave;
  149.                 default:
  150.                         break;
  151.               }
  152.           }
  153. leave:  printf("\x1B[0m");
  154.         printf("\x1B[2J");              /* CLEAR SCREEN */
  155. }
  156.  
  157. /* SET CURSOR POSITION */
  158.  
  159. set_curs(row, col)
  160. int row, col;
  161.  
  162. {
  163.         union REGS regs;
  164.  
  165.         regs.h.ah = 2;                  /* SET POSITION */
  166.         regs.h.bh = 0;                  /* FOR PAGE 0   */
  167.         regs.h.dh = row;                /* CURRENT ROW  */
  168.         regs.h.dl = col;                /* CURRENT COL  */
  169.         int86(0x10, ®s, ®s);      /* INTERRUPT 10h*/
  170. }
  171.  
  172. /* SET ATTRIBUTES */
  173.  
  174. set_attr(character, fore, back, count)
  175. char character;
  176. int fore, back, count;
  177.  
  178. {
  179.         union REGS regs;
  180.                                 /* IF BIT 7 IS 1, XOR CHARACTER ONTO SCREEN */
  181.  
  182.         regs.h.ah = 0x09;
  183.         regs.h.bh = 0;
  184.         regs.x.cx = count;
  185.         regs.h.al = character;
  186.         regs.h.bl = fore | back << 4;
  187.         int86(0x10, ®s, ®s);      /* INTERRUPT 10h*/
  188. }
  189.  
  190. /* FUNCTION TO GET USER INPUT */
  191.  
  192. int getcode()
  193.  
  194. {       int key;
  195.  
  196.         if((key=getch()) != 0) return(key);
  197.         return(getch());
  198. }
  199.  
  200. /* TONE */
  201.  
  202. tone()
  203.  
  204. {
  205.         int     highbt, lowbt, port;
  206.         long    i, count, divisor;
  207.  
  208.         divisor = FREQSCALE / freq;
  209.         highbt  = divisor / 256;
  210.         lowbt   = divisor % 256;
  211.         count   = TIMESCALE * duration;
  212.         outp(TMODE_PORT, TMODE);
  213.         outp(FREQ_PORT, lowbt);
  214.         outp(FREQ_PORT, highbt);
  215.         port = inp(BEEP_PORT);
  216.         outp(BEEP_PORT, ON);
  217.         for(i=1;i<count;i++);
  218.         outp(BEEP_PORT, port);
  219. }
  220.